﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename : book_list_page.cs
//        description :
//
//        created by 周济 at 2011/2/13 10:12:57
//        mail:80406241@qq.com
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.virtualWeb.book
{
    public partial class book_list_page : System.Web.UI.Page
    {
        D_T_EBook_Exp _dtsc_exp = new D_T_EBook_Exp();
        virtualWeb.uc.book.book_tabs UC = new huaguwang.virtualWeb.uc.book.book_tabs();
        //private class clsPara
        //{
        //    public int iType { get; set; }
        //    public bool order1 { get; set; }
        //    public bool order2 { get; set; }
        //}
        /// <summary>
        /// 查询的实现方法
        /// </summary>
        /// <param name="begin"></param>
        private void SetData(int begin)
        {
            AdminPublic.Try(() =>
            {

                SetLiteral(_dtsc_exp.GetPage1Size(M_Type));
                this.Repeater1.DataSource = _dtsc_exp.GetPage1(
                    (M_Page - 1) * M_PageSize,
                    M_PageSize,
                    M_Type,
                    m =>
                    {
                        UC.ToPage(ref m, 8, 30, 60, "地址１/file.aspx?id=", "地址１/file.aspx?id=");
                        return m;
                    }
                    );
                this.Repeater1.DataBind();
            });
        }
        /// <summary>
        /// 产生分页标记
        /// </summary>
        /// <param name="pageCount">记录总数</param>
        private void SetLiteral(int pageCount)
        {
            var blnB = true;
            var blnE = true;
            // 数据集的数量
            var intSum = pageCount;
            //计算总页数
            var intPage = intSum / M_PageSize + ((intSum % M_PageSize) > 0 ? 1 : 0);
            //处理当前的页号
            if(M_Page <= 0)
            {
                M_Page = 1;
            }
            if(M_Page > intPage)
            {
                M_Page = intPage;
            }
            //判断跳页标签是否出现
            if(intPage <= M_Size)
            {
                //如果原始分页数量小于等于１０则没有头尾
                blnB = false;
                blnE = false;
            }
            else
            {
                if(M_Page <= M_Size)
                {
                    blnB = false;
                }
                else
                {
                    blnB = true;
                }
                if(M_Page >= (intPage / M_Size * M_Size))
                {
                    blnE = false;
                }
                else
                {
                    blnE = true;
                }
            }

            //开始产生标签
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<a href='javascript:goPage(" + 1 + ");' >首页</a>|");
            sb.Append("<a href='javascript:goPage(" + (M_Page - 1) + ");' >上一页</a>|");
            //产生首跳页
            var tempB = (M_Page / M_Size * M_Size + 1);
            var tempE = (M_Page / M_Size * M_Size + M_Size);
            if(M_Page % M_Size == 0)
            {
                tempB = M_Page - M_Size + 1;
                tempE = M_Page;
            }
            if(tempE > intPage)
            {
                tempE = intPage;
            }
            if(blnB)
            {
                sb.Append("<a href='javascript:goPage(" + (tempB - 1) + ");' title='前" + M_Size + "页' >...</a>|");
            }
            //产生中间页号
            for(int i = tempB; i <= tempE; i++)
            {
                if(i == M_Page)
                {
                    sb.Append("<a href='javascript:goPage(" + i + ");' ><b>" + i + "</b></a>|");
                }
                else
                {
                    sb.Append("<a href='javascript:goPage(" + i + ");' >" + i + "</a>|");
                }
            }
            //产生尾跳页
            if(blnE)
            {
                sb.Append("<a href='javascript:goPage(" + (tempE + 1) + ");' title='后" + M_Size + "页' >...</a>|");
            }
            sb.Append("<a href='javascript:goPage(" + (M_Page + 1) + ");' >下一页</a>|");
            sb.Append("<a href='javascript:goPage(" + intPage + ");' >尾页</a>|");

            this.Literal_Page.Text = sb.ToString();
        }
        /// <summary>
        /// 查询类型
        /// </summary>
        private int M_Type = 0;
        /// <summary>
        /// 当前页数
        /// </summary>
        private int M_Page = 0;
        /// <summary>
        /// 分页数量
        /// </summary>
        private readonly int M_PageSize = 1;
        /// <summary>
        /// 每次显示的页数
        /// </summary>
        private readonly int M_Size = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            M_Type = Request.Params["orderpage"].Exp_IntTryParse();
            M_Page = Request.Params["thispage"].Exp_IntTryParse();
            Response.Clear();
            Response.ContentType = "text/plain";
            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");

            /*
            注意，目前还没有把排序的东西做起来。
             * 购买，等统计资料也没有做
            */
            SetData(0);
        }

    }
}
